﻿using Corina.Commands;
using Corina.Common;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class PendingUserViewmodel : ViewmodelBase
    {
        public PendingUserViewmodel()
        {

            UserClient.GetPendingUserCompleted += GetPendingUserCompleted;
            UserClient.UpdateUserStatusCompleted += UpdateUserStatusCompleted;
            UserClient.RemoveUserCompleted+=RemoveUserCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        private void RemoveUserCompleted(object sender, DataService.RemoveUserCompletedEventArgs e)
        {
            LoadData();
        }

        private void UpdateUserStatusCompleted(object sender, DataService.UpdateUserStatusCompletedEventArgs e)
        {
            LoadData();
        }

        private void GetPendingUserCompleted(object sender, DataService.GetPendingUserCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                if(e.Result.Result == ResultValue.Succeed)
                {
                    var items = (from t in e.Result.DataObjects.AsEnumerable()
                                 select new
                                 {
                                     Id = t["Id"],
                                     Name = t["Name"],
                                     Account = t["Account"],
                                     RegisterTime = (DateTime)t["RegisterTime"],
                                     Kind = t["UserKind"],
                                     Industry = t["Industry"]
                                 }
                                 );
                    ItemsCollection = new ObservableCollection<object>(items);
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        }

        #region Methods
        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            UserClient.GetPendingUserAsync(Account, Password);
        }

        #endregion

        #region Collection
        private ObservableCollection<object> _itemsCollection;
        public ObservableCollection<object> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }
        #endregion

        #region Commands
        public ICommand ApproveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    UserClient.UpdateUserStatusAsync(Account, Password, id, AccountStatusValue.Normal);
                });
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    if (ShowConfirm(Language.LanguageKeys.IDS_DELETE_CONFIRM) == MessageBoxResult.OK)
                    {
                        UserClient.RemoveUserAsync(Account, Password, id);
                    }
                });
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        LoadData();
                    }
                };
            }
        }
        #endregion
    }
}
